/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package ringitupscoringcalculator;

/**
 *
 * @author Jacob Burroughs
 */
public class TicTacToeBoard {
    private final static int RED = 1;
    private final static int BLUE = 2;
    private final static int NONE = 0;
    private int[][] redRings = new int[3][3];
    private int[][] blueRings = new int[3][3];
    private boolean[][] redAutoRings = new boolean[3][3];
    private boolean[][] blueAutoRings = new boolean[3][3];
    private int[][] ringControl = new int[3][3];
    private char irColumn='A';
    
    /**
     * Default constructor creating an empty pegboard with the IR Beacon on column A
     */
    public TicTacToeBoard()
    {
        
    }

    /**
     * Mutator method setting the column with an IR Beacon
     * @param column Uppercase character expressing the column
     */
    public void setIRColumn(char column)
    {
        irColumn=column;
    }
    /**
     * Mutator method setting the number of red rings on a given peg
     * @param xPos The x-coordinate of the peg starting with 0 at the top
     * @param yPos The y-coordinate of the peg starting with 0 for column A
     * @param ringCount The number of red rings on the peg
     */
    public void setRedRingCount(int xPos, int yPos, int ringCount)
    {
        redRings[yPos][xPos]=ringCount;
        setOwnership(xPos,yPos);
    }
    /**
     * Mutator method setting the number of blue rings on a given peg
     * @param xPos The x-coordinate of the peg starting with 0 at the top
     * @param yPos The y-coordinate of the peg starting with 0 for column A
     * @param ringCount The number of blue rings on the peg
     */
    public void setBlueRingCount(int xPos, int yPos, int ringCount)
    {
        blueRings[yPos][xPos]=ringCount;
        setOwnership(xPos,yPos);
    }
    /**
     *
     * @param xPos
     * @param yPos
     * @param state
     */
    public void setBlueAutonomousRing(int xPos, int yPos, boolean state)
    {
        blueAutoRings[yPos][xPos]=state;
        setOwnership(xPos,yPos);
    }
    /**
     *
     * @param xPos
     * @param yPos
     * @param state
     */
    public void setRedAutonomousRing(int xPos, int yPos, boolean state)
    {
        redAutoRings[yPos][xPos]=state;
        setOwnership(xPos,yPos);
    }
    private int setOwnership(int xPos, int yPos)
    {
        int control=NONE;
        if(redRings[yPos][xPos]>blueRings[yPos][xPos])
        {
            control=RED;
        }
        else if(redRings[yPos][xPos]<blueRings[yPos][xPos])
        {
            control=BLUE;
        }
        if(redAutoRings[yPos][xPos]&&!blueAutoRings[yPos][xPos])
        {
            control=RED;
        }
        else if(!redAutoRings[yPos][xPos]&&blueAutoRings[yPos][xPos])
        {
            control=BLUE;
        }
        ringControl[yPos][xPos]=control;
        return control;
    }
    private static int sumArray(int myArray[]) {
        int i, sum = 0;
        // Write a loop to step through the array and sum the elements
        for (i = 0; i < myArray.length; i++) {
            sum = sum + myArray[i];
        }
        // Use a return statement to send back the calculated reault
        return sum;
    }
    private int checkRedAuto()
    {
        int currScore=0;
        int column = irColumn-65;
        if(redAutoRings[0][column])
        {
            currScore=currScore+50;
        }
        if(redAutoRings[1][column])
        {
            currScore=currScore+50;
        }
        if(redAutoRings[2][column])
        {
            currScore=currScore+50;
        }
        return currScore;
    }
    private int checkBlueAuto()
    {
        int currScore=0;
        int column = irColumn-65;
        if(blueAutoRings[0][column])
        {
            currScore=currScore+50;
        }
        if(blueAutoRings[1][column])
        {
            currScore=currScore+50;
        }
        if(blueAutoRings[2][column])
        {
            currScore=currScore+50;
        }
        return currScore;
    }
    /**
     * Accessor method returning the red ring score 
     * @return the red ring score
     */
    public int getRedRingScore()
    {
        int currScore=sumArray(redRings[0])*15+sumArray(redRings[1])*10+sumArray(redRings[2])*5;
        if(ringControl[0][0]==RED&&ringControl[0][1]==RED&&ringControl[0][2]==RED)
        {
            currScore=currScore+30;
        }
        if(ringControl[1][0]==RED&&ringControl[1][1]==RED&&ringControl[1][2]==RED)
        {
            currScore=currScore+30;
        }
        if(ringControl[2][0]==RED&&ringControl[2][1]==RED&&ringControl[2][2]==RED)
        {
            currScore=currScore+30;
        }
        if(ringControl[0][0]==RED&&ringControl[1][1]==RED&&ringControl[2][2]==RED)
        {
            currScore=currScore+30;
        }
        if(ringControl[0][0]==RED&&ringControl[1][0]==RED&&ringControl[2][0]==RED)
        {
            currScore=currScore+30;
        }
        if(ringControl[0][1]==RED&&ringControl[1][1]==RED&&ringControl[2][1]==RED)
        {
            currScore=currScore+30;
        }
        if(ringControl[0][2]==RED&&ringControl[1][2]==RED&&ringControl[2][2]==RED)
        {
            currScore=currScore+30;
        }
        if(ringControl[0][2]==RED&&ringControl[1][1]==RED&&ringControl[2][0]==RED)
        {
            currScore=currScore+30;
        }
        currScore=currScore+checkRedAuto();
        return currScore;
    }
    /**
     * Accessor method returning the blue ring score 
     * @return The blue ring score
     */
    public int getBlueRingScore()
    {
        int currScore=sumArray(blueRings[0])*15+sumArray(blueRings[1])*10+sumArray(blueRings[2])*5;
        if(ringControl[0][0]==BLUE&&ringControl[0][1]==BLUE&&ringControl[0][2]==BLUE)
        {
            currScore=currScore+30;
        }
        if(ringControl[1][0]==BLUE&&ringControl[1][1]==BLUE&&ringControl[1][2]==BLUE)
        {
            currScore=currScore+30;
        }
        if(ringControl[2][0]==BLUE&&ringControl[2][1]==BLUE&&ringControl[2][2]==BLUE)
        {
            currScore=currScore+30;
        }
        if(ringControl[0][0]==BLUE&&ringControl[1][1]==BLUE&&ringControl[2][2]==BLUE)
        {
            currScore=currScore+30;
        }
        if(ringControl[0][0]==BLUE&&ringControl[1][0]==BLUE&&ringControl[2][0]==BLUE)
        {
            currScore=currScore+30;
        }
        if(ringControl[0][1]==BLUE&&ringControl[1][1]==BLUE&&ringControl[2][1]==BLUE)
        {
            currScore=currScore+30;
        }
        if(ringControl[0][2]==BLUE&&ringControl[1][2]==BLUE&&ringControl[2][2]==BLUE)
        {
            currScore=currScore+30;
        }
        if(ringControl[0][2]==BLUE&&ringControl[1][1]==BLUE&&ringControl[2][0]==BLUE)
        {
            currScore=currScore+30;
        }
        currScore=currScore+checkBlueAuto();
        return currScore;
    }
}
